package bb.lanxing.view.dialog;

import android.app.Dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import java.util.Objects;

import bb.lanxing.R;
import bb.lanxing.utils.ScreenUtil;


/**
 * 自定义弹出框
 * 记得写的要灵活,没事多看看设计模式,需要灵活设计
 * 对修改封闭,对拓展开放
 */
public abstract class BaseDialogFragment extends DialogFragment {
    private static final String TAG = "BaseDialogFragment";
    @StyleRes
    protected int theme = R.style.bottom_dialog; // dialog主题
    private int mGravity = Gravity.CENTER;
    private int mPadding = 0;
    private int mMargin = 0;

    protected BaseDialogFragment(@LayoutRes int layoutRes) {
        super(layoutRes);
        setRetainInstance(true);
    }

    public BaseDialogFragment setPadding(int padding) {
        mPadding = padding;
        return this;
    }

    public BaseDialogFragment setMargin(int margin) {
        mMargin = margin;
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, theme);
    }

    @Override
    public void onStart() {
        super.onStart();
        initDialog();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        bindView(view, this);
        return view;
    }

    public abstract void bindView(View view, DialogFragment dialogFragment);

    private void initDialog() {
        Window window = Objects.requireNonNull(getDialog()).getWindow();
        if (null != window) {
            window.setBackgroundDrawableResource(R.drawable.dialog_bg);
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.gravity = mGravity;
            attributes.width = ScreenUtil.getScreenWidth(requireContext()) - 2 * mMargin;
            attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(attributes);
            // 设置padding, 需要在 show 后面设置有效
            View view = window.getDecorView();
            view.setPadding(mPadding, mPadding, mPadding, mPadding);
        }
    }

    public void show(FragmentManager fragmentManager) {
        if (!isAdded()) {
            show(fragmentManager, TAG);
        }
    }

    public void setGravity(int gravity) {
        mGravity = gravity;
    }

    @Override
    public void onDestroyView() {
        Dialog dialog = getDialog();
        // handles https://code.google.com/p/android/issues/detail?id=17423
        if (dialog != null && getRetainInstance()) {
            dialog.setDismissMessage(null);
        }
        super.onDestroyView();
    }
}
